package com.wenx.webservicecamel;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.tree.DefaultText;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;

public class AppWebServicePub1 extends RouteBuilder {

    public static void main(String[] args) throws Exception {

        CamelContext context = new DefaultCamelContext(); // 1. 创建 CamelContext.
        context.addRoutes(new AppWebServicePub1());
        context.setTracing(true);
        context.start(); // 4. 启动CamelContext.
        Thread.sleep(Integer.MAX_VALUE);  // 为了保持CamelContext处于工作状态，这里需要sleep主线程
        context.stop(); // 最后停止CamelContext
    }

    /**
     * 生产消费   webservice---camel---webservice
     *
     * @throws Exception
     */
    public void configure() throws Exception {
        String content = "<?xml version=\"1.0\" encoding=\"utf-8\"?>  \n" +
                "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body>        \n" +
                "<getUserDataByIdAndName xmlns=\"http://ws.services.example.com/\"> <name>aaa</name><id>123</id></getUserDataByIdAndName>     \n" +
                "</soap:Body> </soap:Envelope>";
        String context2 = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ws=\"http://ws.services.example.com/\">\n" +
                "   <soapenv:Header/>\n" +
                "   <soapenv:Body>\n" +
                "      <ws:getUserDataByIdAndName>\n" +
                "         <id>123</id>\n" +
                "         <!--Optional:-->\n" +
                "         <name>aaa</name>\n" +
                "      </ws:getUserDataByIdAndName>\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>";
        from("timer:foo?repeatCount=1")
                .setHeader(Exchange.CHARSET_NAME, constant("UTF-8"))
                .setBody(constant(context2))             //set request body
                .to("cxf:"
                                + "http://10.10.3.162:8087/cxf/ws/getUserInfo" //service address
                                + "?"
                                + "wsdlURL=http://10.10.3.162:8087/cxf/ws/getUserInfo?wsdl"    //wsdl url
                                + "&"
                                + "dataFormat=RAW"        //dataformat type
                )
                .setHeader(Exchange.CHARSET_NAME, constant("utf-8"))
                .convertBodyTo(String.class)
                .process(new Processor() {
                    @Override
                    public void process(Exchange exchange) throws Exception {
                        String objxml = exchange.getIn().getBody().toString();
                        System.out.println("objxml = " + objxml);
                    }
                });
    }
}
